<template>
    <div class="p-2">
        <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
            :leave-active-class="proxy?.animate.searchAnimate.leave">
            <div v-show="showSearch" class="mb-[10px]">
                <el-card shadow="hover">
                    <el-form ref="queryFormRef" :model="data.queryParams" :inline="true">
                        <el-form-item label="资源名称" prop="resourceName">
                            <el-input v-model="data.queryParams.resourceName" placeholder="请输入模板名称" clearable
                                @keyup.enter="handleQuery" />
                        </el-form-item>
                        <el-form-item label="资源分类" prop="resourceType">
                            <el-select v-model="data.queryParams.resourceType" :empty-values="[null, undefined]"
                                placeholder="请选择资源分类" clearable @visible-change="getType">
                                <el-option label="全部" value=""></el-option>
                                <el-option v-for="dict in typeList" :key="dict.resourceId"
                                    :label="dict.resourceTypeName" :value="dict.resourceId" />
                            </el-select>

                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                        </el-form-item>
                    </el-form>
                </el-card>
            </div>
        </transition>

        <el-card shadow="hover">
            <template #header>
                <el-row :gutter="10">
                    <el-col :span="1.5">
                        <el-button type="primary" :icon="Plus" plain @click="handleAdd">新增</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="danger" :icon="Delete" plain @click="handleDelete"
                            :disabled="selectionList.length === 0">删除</el-button>
                    </el-col>
                    <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
                </el-row>
            </template>

            <el-table ref="roleTableRef" v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="资源名称" prop="resourceName" :show-overflow-tooltip="true" />
                <el-table-column label="资源类型" prop="resourceTypeName" :show-overflow-tooltip="true" />
                <el-table-column label="描述" prop="resourceDescribe" :show-overflow-tooltip="true" />
                <el-table-column label="操作" align="center">
                    <template #default="scope">
                        <el-button type="primary" :icon="Edit" size="small" @click="editRow(scope.row)" />
                        <el-button type="danger" :icon="Delete" size="small" @click="delRow(scope.row)" />
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="data.queryParams.total > 0" v-model:page="data.queryParams.pageNum"
                v-model:limit="data.queryParams.pageSize" :total="data.queryParams.total" @pagination="getList" />
        </el-card>
        <el-dialog v-model="dialogVisible" :title="actionType === 'add' ? '新增' : '修改'" width="500"
            :before-close="handleClose">
            <el-form :model="addForm" ref="ruleFormRef" label-width="106px" :rules="rules">
                <el-form-item label="资源名称" prop="resourceName">
                    <el-input v-model="addForm.resourceName" placeholder="请输入资源名称" />
                </el-form-item>
                <el-form-item label="资源分类" prop="resourceType">
                    <el-select v-model="addForm.resourceType" placeholder="请选择资源分类" clearable @visible-change="getType">
                        <el-option v-for="dict in typeList" :key="dict.resourceId" :label="dict.resourceTypeName"
                            :value="dict.resourceId" />
                    </el-select>
                </el-form-item>
                <el-form-item label="描述" prop="resourceDescribe">
                    <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" v-model="addForm.resourceDescribe"
                        placeholder="请输入资源类型名称" />
                </el-form-item>
                <el-form-item>

                    <el-button @click="handleClose">取消</el-button>
                    <el-button type="primary" @click="submitForm(ruleFormRef)">
                        确定
                    </el-button>
                </el-form-item>
            </el-form>

        </el-dialog>
    </div>
</template>

<script setup lang="ts">
import { getResourceList, addResource, deleteResource, updateResource, getResourceTypeList } from "@/api/resources/calendarManagementApi"
import { Delete, Plus, Edit } from '@element-plus/icons-vue'
const { proxy } = getCurrentInstance() as ComponentInternalInstance
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'

const loading = ref(true)
const showSearch = ref(true)
const dataList = ref([])
const selectionList = ref([])
const typeList = ref([])
const dialogVisible = ref(false)
const actionType = ref('add')

const data = reactive({
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        resourceName: '',
        resourceType: "",
    },
})
const addForm = reactive({
    resourceName: '',
    resourceType: '',
    resourceDescribe: '',
    resourceTypeName: '',
})
const ruleFormRef = ref()
const rules = reactive({
    resourceName: [{ required: true, message: '请输入资源名称', trigger: 'blur' },],
    resourceType: [{ required: true, message: '请选择资源分类', trigger: 'change' },],

})




/**
 * 查询模板列表
 */
const getList = async () => {
    loading.value = true
    const res: any = await getResourceList({
        pageNum: data.queryParams.pageNum,
        pageSize: data.queryParams.pageSize,
        resourceName: data.queryParams.resourceName,
        resourceType: data.queryParams.resourceType

    })
    if (res.code == 200) {
        loading.value = false
        dataList.value = res.data.rows
        data.queryParams.total = res.data.total
    }
}
// 查询类型
const getType = async () => {
    const res: any = await getResourceTypeList({
        pageNum: data.queryParams.pageNum,
        pageSize: data.queryParams.pageSize,
    })
    if (res.code == 200) {
        loading.value = false
        typeList.value = res.data
    }
}
/**
 * 搜索按钮操作
 */
const handleQuery = () => {
    data.queryParams.pageNum = 1
    getList()
}

/** 重置 */
const resetQuery = () => {
    data.queryParams.resourceName = ''
    data.queryParams.resourceType = ''
    handleQuery()
}
const handleSelectionChange = (val) => {
    selectionList.value = val
}
const handleAdd = () => {
    actionType.value = 'add'
    dialogVisible.value = true
}
const handleClose = () => {
    dialogVisible.value = false
    ruleFormRef.value?.resetFields()
}

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            if (actionType.value == 'add') {
                addResource(addForm).then((res) => {
                    if (res.code == 200) {
                        ElMessage.success('新增成功')
                        handleClose()
                        getList()
                    }
                })
            } else {
                updateResource(addForm).then((res) => {
                    if (res.code == 200) {
                        ElMessage.success('修改成功')
                        handleClose()
                        getList()
                    }
                })
            }
        } else {
            console.log('error submit!', fields)
        }
    })
}
/**修改按钮操作 */
const editRow = (row) => {
    actionType.value = 'edit'
    dialogVisible.value = true
    getType()
    Object.assign(addForm, row)
}
const delRow = (row) => {
    ElMessageBox.confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(async () => {
        const res = await deleteResource(row.resourceId)
        if (res.code == 200) {
            ElMessage.success('删除成功')
            getList()
        }
    })
}
/**删除按钮操作 */
const handleDelete = async () => {
    ElMessageBox.confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(async () => {
        const ids = selectionList.value.map((item) => item.resourceId)
        const res = await deleteResource(ids)
        if (res.code == 200) {
            ElMessage.success('删除成功')
            getList()
        }
    }).catch(() => {
        ElMessage.info('已取消删除')
    })
}


onMounted(() => {
    getList()
})
</script>